import  requests

#梨视频爬取
def getContent(url):
    agent={
       "User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36",
        "Referer": "https://www.pearvideo.com/video_1744481"
    }
    try:
        session=requests.session()
        response=session.get(url=url,headers=agent)
        response.raise_for_status()
        response.encoding=response.apparent_encoding
    except Exception as e:
        print("爬取错误")
    else :
        response.close()
        return  response
if __name__ == '__main__':
    url="https://www.pearvideo.com/video_1744481"
    number=url.split("_")[1]
    #视频地址
    videoUrl=f"https://www.pearvideo.com/videoStatus.jsp?contId={number}"
    htmlContent=getContent(videoUrl)
    dic=htmlContent.json()
    videoUrl=dic['videoInfo']['videos']['srcUrl']
    systemTime=dic['systemTime']
    videoUrl=videoUrl.replace(systemTime,f"cont-{number}")
    #print(videoUrl)
    with open("resouce/video/pearVideo.mp4",mode="wb" ) as f:
        f.write(requests.get(videoUrl).content)


